package com.upb.webserver.overview;

import cn.hutool.core.collection.CollectionUtil;
import com.upb.webserver.application.ApplicationManager;
import com.upb.webserver.chain.ChainManager;
import com.upb.webserver.chain.ChainService;
import com.upb.webserver.common.enums.ChainOrAppStatusEnum;
import com.upb.webserver.common.enums.PlatformEnum;
import com.upb.webserver.common.exception.WebServerException;
import com.upb.webserver.common.pojo.overview.RspOverviewVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.company.CompanyDeveloperService;
import com.upb.webserver.host.HostService;
import com.upb.webserver.org.OrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Set;

@Slf4j
@Service
public class OverviewService {

    @Autowired
    private CompanyDeveloperService companyDeveloperService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private HostService hostService;
    @Autowired
    private ChainService chainService;
    @Autowired
    private ChainManager chainManager;
    @Autowired
    private ApplicationManager applicationManager;

    /**
     * @param platform
     * @param currentAccountId
     * @return
     */
    public RspOverviewVO getOverView(byte platform, int currentAccountId) {
        log.info("start exec method [getOverView]. platform:{} currentAccountId:{}", platform, currentAccountId);

        if (platform != PlatformEnum.DEVELOPER.getId()) {
            log.warn("fail exec method [getOverView]. request from platform:{} ,but support:{}", platform, PlatformEnum.DEVELOPER.getId());
            throw new WebServerException(ConstantCode.PLATFORM_NOT_SUPPORT);
        }

        int companyId = companyDeveloperService.selectCompanyIdByDeveloperId(currentAccountId);

        //get org List
        Set<Integer> orgIdSet = orgService.getOrgIdSetByCompanyId(companyId);
        RspOverviewVO overviewVO = new RspOverviewVO();
        if (CollectionUtil.isEmpty(orgIdSet)) {
            log.info("finish exec method [getOverView]. not found org record by companyId:{} result:{}", companyId, JsonUtils.objToString(overviewVO));
            return overviewVO;
        }
        overviewVO.setOrgCount(orgIdSet.size());

        //get host list
        Set<Integer> hostIdSet = hostService.getHostIdSetByCompanyId(companyId);
        if (CollectionUtil.isEmpty(hostIdSet)) {
            log.info("finish exec method [getOverView]. not found host record by companyId:{} result:{}", companyId, JsonUtils.objToString(overviewVO));
            return overviewVO;
        }
        overviewVO.setHostCount(hostIdSet.size());

        //get chain list
        Set<Integer> chainIdSet = chainManager.getChainIdSetByCompanyIdList(Arrays.asList(companyId));
        if (CollectionUtil.isEmpty(chainIdSet)) {
            log.info("finish exec method [getOverView]. not found chain record by companyId:{} result:{}", companyId, JsonUtils.objToString(overviewVO));
            return overviewVO;
        }
        long chainCount = chainManager.countByChainIdListAndStatus(CollectionUtil.newArrayList(chainIdSet), ChainOrAppStatusEnum.RUNNING.getId());
        overviewVO.setChainCount((int) chainCount);

        //get application list
        //get chain list
        Set<Integer> applicationIdSet = applicationManager.getApplicationIdSetByCompanyIdList(Arrays.asList(companyId));
        if (CollectionUtil.isEmpty(chainIdSet)) {
            log.info("finish exec method [getOverView]. not found application record by companyId:{} result:{}", companyId, JsonUtils.objToString(overviewVO));
            return overviewVO;
        }

        long appCount = applicationManager.countByApplicationIdListAndStatus(CollectionUtil.newArrayList(applicationIdSet), ChainOrAppStatusEnum.RUNNING.getId());
        overviewVO.setApplicationCount((int) appCount);

        log.info("success exec method [getOverView].result:{}", platform, currentAccountId);
        return overviewVO;
    }
}
